package mips;

import mips.element.MipsElement;
import mips.element.Section;

import java.util.ArrayList;

public class Function {
    private Section label;
    private ArrayList<MipsElement> states;
    private ArrayList<MipsElement> prepare;
    private ArrayList<MipsElement> init;
    private ArrayList<MipsElement> ending;

    public Function(String name) {
        this.label = new Section(name);
        this.states = new ArrayList<>();
        this.prepare = new ArrayList<>();
        this.init = new ArrayList<>();
        this.ending = new ArrayList<>();
    }

    public String getName() {
        return this.label.getName();
    }

    public void addState(MipsElement state) {
        this.states.add(state);
    }

    public void addPrepare(MipsElement element) {
        this.prepare.add(element);
    }

    public void addInit(MipsElement element) {
        this.init.add(element);
    }

    public void addEnding(MipsElement element) {
        this.ending.add(element);
    }

    public String toString() {
        String str = label.toString() + "\n";
        if (!prepare.isEmpty()) {
            for (MipsElement mipsElement: prepare) {
                str += mipsElement.toString() + "\n";
            }
        }
        if (!init.isEmpty()) {
            for (MipsElement mipsElement: init) {
                str += mipsElement.toString() + "\n";
            }
        }
        if (!states.isEmpty()) {
            for (MipsElement mipsElement: states) {
                str += mipsElement.toString() + "\n";
            }
        }
        Section section = new Section(label.getName() + "_end");
        str += section.toString() + "\n";
        if (!ending.isEmpty()) {
            for (MipsElement mipsElement: ending) {
                str += mipsElement.toString() + "\n";
            }
        }
        return str;
    }
}
